// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.Security;

[JsonConverter(typeof(ApiKeyAggregationConverter))]
public sealed partial class ApiKeyAggregation
{
	internal ApiKeyAggregation(string variantName, object variant)
	{
		if (variantName is null)
			throw new ArgumentNullException(nameof(variantName));
		if (variant is null)
			throw new ArgumentNullException(nameof(variant));
		if (string.IsNullOrWhiteSpace(variantName))
			throw new ArgumentException("Variant name must not be empty or whitespace.");
		VariantName = variantName;
		Variant = variant;
	}

	internal object Variant { get; }
	internal string VariantName { get; }

	public static ApiKeyAggregation Cardinality(Elastic.Clients.Elasticsearch.Serverless.Aggregations.CardinalityAggregation cardinalityAggregation) => new ApiKeyAggregation("cardinality", cardinalityAggregation);
	public static ApiKeyAggregation Composite(Elastic.Clients.Elasticsearch.Serverless.Aggregations.CompositeAggregation compositeAggregation) => new ApiKeyAggregation("composite", compositeAggregation);
	public static ApiKeyAggregation DateRange(Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeAggregation dateRangeAggregation) => new ApiKeyAggregation("date_range", dateRangeAggregation);
	public static ApiKeyAggregation Filter(Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyQuery apiKeyQueryContainer) => new ApiKeyAggregation("filter", apiKeyQueryContainer);
	public static ApiKeyAggregation Filters(Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyFiltersAggregation apiKeyFiltersAggregation) => new ApiKeyAggregation("filters", apiKeyFiltersAggregation);
	public static ApiKeyAggregation Missing(Elastic.Clients.Elasticsearch.Serverless.Aggregations.MissingAggregation missingAggregation) => new ApiKeyAggregation("missing", missingAggregation);
	public static ApiKeyAggregation Range(Elastic.Clients.Elasticsearch.Serverless.Aggregations.RangeAggregation rangeAggregation) => new ApiKeyAggregation("range", rangeAggregation);
	public static ApiKeyAggregation Terms(Elastic.Clients.Elasticsearch.Serverless.Aggregations.TermsAggregation termsAggregation) => new ApiKeyAggregation("terms", termsAggregation);
	public static ApiKeyAggregation ValueCount(Elastic.Clients.Elasticsearch.Serverless.Aggregations.ValueCountAggregation valueCountAggregation) => new ApiKeyAggregation("value_count", valueCountAggregation);

	/// <summary>
	/// <para>
	/// Sub-aggregations for this aggregation.
	/// Only applies to bucket aggregations.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("aggregations")]
	public IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregation>? Aggregations { get; set; }
	[JsonInclude, JsonPropertyName("meta")]
	public IDictionary<string, object>? Meta { get; set; }

	public bool TryGet<T>([NotNullWhen(true)] out T? result) where T : class
	{
		result = default;
		if (Variant is T variant)
		{
			result = variant;
			return true;
		}

		return false;
	}
}

internal sealed partial class ApiKeyAggregationConverter : JsonConverter<ApiKeyAggregation>
{
	public override ApiKeyAggregation Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
		{
			throw new JsonException("Expected start token.");
		}

		object? variantValue = default;
		string? variantNameValue = default;
		IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregation>? aggregationsValue = default;
		IDictionary<string, object>? metaValue = default;
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType != JsonTokenType.PropertyName)
			{
				throw new JsonException("Expected a property name token.");
			}

			if (reader.TokenType != JsonTokenType.PropertyName)
			{
				throw new JsonException("Expected a property name token representing the name of an Elasticsearch field.");
			}

			var propertyName = reader.GetString();
			reader.Read();
			if (propertyName == "aggregations")
			{
				aggregationsValue = JsonSerializer.Deserialize<IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregation>?>(ref reader, options);
				continue;
			}

			if (propertyName == "meta")
			{
				metaValue = JsonSerializer.Deserialize<IDictionary<string, object>?>(ref reader, options);
				continue;
			}

			if (propertyName == "cardinality")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.CardinalityAggregation?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "composite")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.CompositeAggregation?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "date_range")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeAggregation?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "filter")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyQuery?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "filters")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyFiltersAggregation?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "missing")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.MissingAggregation?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "range")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.RangeAggregation?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "terms")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.TermsAggregation?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "value_count")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.ValueCountAggregation?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			throw new JsonException($"Unknown property name '{propertyName}' received while deserializing the 'ApiKeyAggregation' from the response.");
		}

		var result = new ApiKeyAggregation(variantNameValue, variantValue);
		result.Aggregations = aggregationsValue;
		result.Meta = metaValue;
		return result;
	}

	public override void Write(Utf8JsonWriter writer, ApiKeyAggregation value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		if (value.Aggregations is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, value.Aggregations, options);
		}

		if (value.Meta is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, value.Meta, options);
		}

		if (value.VariantName is not null && value.Variant is not null)
		{
			writer.WritePropertyName(value.VariantName);
			switch (value.VariantName)
			{
				case "cardinality":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.CardinalityAggregation>(writer, (Elastic.Clients.Elasticsearch.Serverless.Aggregations.CardinalityAggregation)value.Variant, options);
					break;
				case "composite":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.CompositeAggregation>(writer, (Elastic.Clients.Elasticsearch.Serverless.Aggregations.CompositeAggregation)value.Variant, options);
					break;
				case "date_range":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeAggregation>(writer, (Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeAggregation)value.Variant, options);
					break;
				case "filter":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyQuery>(writer, (Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyQuery)value.Variant, options);
					break;
				case "filters":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyFiltersAggregation>(writer, (Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyFiltersAggregation)value.Variant, options);
					break;
				case "missing":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.MissingAggregation>(writer, (Elastic.Clients.Elasticsearch.Serverless.Aggregations.MissingAggregation)value.Variant, options);
					break;
				case "range":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.RangeAggregation>(writer, (Elastic.Clients.Elasticsearch.Serverless.Aggregations.RangeAggregation)value.Variant, options);
					break;
				case "terms":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.TermsAggregation>(writer, (Elastic.Clients.Elasticsearch.Serverless.Aggregations.TermsAggregation)value.Variant, options);
					break;
				case "value_count":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.Aggregations.ValueCountAggregation>(writer, (Elastic.Clients.Elasticsearch.Serverless.Aggregations.ValueCountAggregation)value.Variant, options);
					break;
			}
		}

		writer.WriteEndObject();
	}
}

public sealed partial class ApiKeyAggregationDescriptor<TDocument> : SerializableDescriptor<ApiKeyAggregationDescriptor<TDocument>>
{
	internal ApiKeyAggregationDescriptor(Action<ApiKeyAggregationDescriptor<TDocument>> configure) => configure.Invoke(this);

	public ApiKeyAggregationDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private ApiKeyAggregationDescriptor<TDocument> Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private ApiKeyAggregationDescriptor<TDocument> Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	private IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregationDescriptor<TDocument>> AggregationsValue { get; set; }
	private IDictionary<string, object>? MetaValue { get; set; }

	/// <summary>
	/// <para>
	/// Sub-aggregations for this aggregation.
	/// Only applies to bucket aggregations.
	/// </para>
	/// </summary>
	public ApiKeyAggregationDescriptor<TDocument> Aggregations(Func<FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregationDescriptor<TDocument>>, FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregationDescriptor<TDocument>>> selector)
	{
		AggregationsValue = selector?.Invoke(new FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregationDescriptor<TDocument>>());
		return Self;
	}

	public ApiKeyAggregationDescriptor<TDocument> Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
	{
		MetaValue = selector?.Invoke(new FluentDictionary<string, object>());
		return Self;
	}

	public ApiKeyAggregationDescriptor<TDocument> Cardinality(Elastic.Clients.Elasticsearch.Serverless.Aggregations.CardinalityAggregation cardinalityAggregation) => Set(cardinalityAggregation, "cardinality");
	public ApiKeyAggregationDescriptor<TDocument> Cardinality(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.CardinalityAggregationDescriptor<TDocument>> configure) => Set(configure, "cardinality");
	public ApiKeyAggregationDescriptor<TDocument> Composite(Elastic.Clients.Elasticsearch.Serverless.Aggregations.CompositeAggregation compositeAggregation) => Set(compositeAggregation, "composite");
	public ApiKeyAggregationDescriptor<TDocument> Composite(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.CompositeAggregationDescriptor<TDocument>> configure) => Set(configure, "composite");
	public ApiKeyAggregationDescriptor<TDocument> DateRange(Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeAggregation dateRangeAggregation) => Set(dateRangeAggregation, "date_range");
	public ApiKeyAggregationDescriptor<TDocument> DateRange(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeAggregationDescriptor<TDocument>> configure) => Set(configure, "date_range");
	public ApiKeyAggregationDescriptor<TDocument> Filter(Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyQuery apiKeyQueryContainer) => Set(apiKeyQueryContainer, "filter");
	public ApiKeyAggregationDescriptor<TDocument> Filter(Action<Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyQueryDescriptor<TDocument>> configure) => Set(configure, "filter");
	public ApiKeyAggregationDescriptor<TDocument> Filters(Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyFiltersAggregation apiKeyFiltersAggregation) => Set(apiKeyFiltersAggregation, "filters");
	public ApiKeyAggregationDescriptor<TDocument> Filters(Action<Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyFiltersAggregationDescriptor<TDocument>> configure) => Set(configure, "filters");
	public ApiKeyAggregationDescriptor<TDocument> Missing(Elastic.Clients.Elasticsearch.Serverless.Aggregations.MissingAggregation missingAggregation) => Set(missingAggregation, "missing");
	public ApiKeyAggregationDescriptor<TDocument> Missing(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.MissingAggregationDescriptor<TDocument>> configure) => Set(configure, "missing");
	public ApiKeyAggregationDescriptor<TDocument> Range(Elastic.Clients.Elasticsearch.Serverless.Aggregations.RangeAggregation rangeAggregation) => Set(rangeAggregation, "range");
	public ApiKeyAggregationDescriptor<TDocument> Range(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.RangeAggregationDescriptor<TDocument>> configure) => Set(configure, "range");
	public ApiKeyAggregationDescriptor<TDocument> Terms(Elastic.Clients.Elasticsearch.Serverless.Aggregations.TermsAggregation termsAggregation) => Set(termsAggregation, "terms");
	public ApiKeyAggregationDescriptor<TDocument> Terms(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.TermsAggregationDescriptor<TDocument>> configure) => Set(configure, "terms");
	public ApiKeyAggregationDescriptor<TDocument> ValueCount(Elastic.Clients.Elasticsearch.Serverless.Aggregations.ValueCountAggregation valueCountAggregation) => Set(valueCountAggregation, "value_count");
	public ApiKeyAggregationDescriptor<TDocument> ValueCount(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.ValueCountAggregationDescriptor<TDocument>> configure) => Set(configure, "value_count");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (AggregationsValue is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsValue, options);
		}

		if (MetaValue is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, MetaValue, options);
		}

		if (!string.IsNullOrEmpty(ContainedVariantName))
		{
			writer.WritePropertyName(ContainedVariantName);
			if (Variant is not null)
			{
				JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
				writer.WriteEndObject();
				return;
			}

			JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class ApiKeyAggregationDescriptor : SerializableDescriptor<ApiKeyAggregationDescriptor>
{
	internal ApiKeyAggregationDescriptor(Action<ApiKeyAggregationDescriptor> configure) => configure.Invoke(this);

	public ApiKeyAggregationDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private ApiKeyAggregationDescriptor Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private ApiKeyAggregationDescriptor Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	private IDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregationDescriptor> AggregationsValue { get; set; }
	private IDictionary<string, object>? MetaValue { get; set; }

	/// <summary>
	/// <para>
	/// Sub-aggregations for this aggregation.
	/// Only applies to bucket aggregations.
	/// </para>
	/// </summary>
	public ApiKeyAggregationDescriptor Aggregations(Func<FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregationDescriptor>, FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregationDescriptor>> selector)
	{
		AggregationsValue = selector?.Invoke(new FluentDescriptorDictionary<string, Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyAggregationDescriptor>());
		return Self;
	}

	public ApiKeyAggregationDescriptor Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
	{
		MetaValue = selector?.Invoke(new FluentDictionary<string, object>());
		return Self;
	}

	public ApiKeyAggregationDescriptor Cardinality(Elastic.Clients.Elasticsearch.Serverless.Aggregations.CardinalityAggregation cardinalityAggregation) => Set(cardinalityAggregation, "cardinality");
	public ApiKeyAggregationDescriptor Cardinality<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.CardinalityAggregationDescriptor> configure) => Set(configure, "cardinality");
	public ApiKeyAggregationDescriptor Composite(Elastic.Clients.Elasticsearch.Serverless.Aggregations.CompositeAggregation compositeAggregation) => Set(compositeAggregation, "composite");
	public ApiKeyAggregationDescriptor Composite<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.CompositeAggregationDescriptor> configure) => Set(configure, "composite");
	public ApiKeyAggregationDescriptor DateRange(Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeAggregation dateRangeAggregation) => Set(dateRangeAggregation, "date_range");
	public ApiKeyAggregationDescriptor DateRange<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeAggregationDescriptor> configure) => Set(configure, "date_range");
	public ApiKeyAggregationDescriptor Filter(Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyQuery apiKeyQueryContainer) => Set(apiKeyQueryContainer, "filter");
	public ApiKeyAggregationDescriptor Filter<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyQueryDescriptor> configure) => Set(configure, "filter");
	public ApiKeyAggregationDescriptor Filters(Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyFiltersAggregation apiKeyFiltersAggregation) => Set(apiKeyFiltersAggregation, "filters");
	public ApiKeyAggregationDescriptor Filters<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Security.ApiKeyFiltersAggregationDescriptor> configure) => Set(configure, "filters");
	public ApiKeyAggregationDescriptor Missing(Elastic.Clients.Elasticsearch.Serverless.Aggregations.MissingAggregation missingAggregation) => Set(missingAggregation, "missing");
	public ApiKeyAggregationDescriptor Missing<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.MissingAggregationDescriptor> configure) => Set(configure, "missing");
	public ApiKeyAggregationDescriptor Range(Elastic.Clients.Elasticsearch.Serverless.Aggregations.RangeAggregation rangeAggregation) => Set(rangeAggregation, "range");
	public ApiKeyAggregationDescriptor Range<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.RangeAggregationDescriptor> configure) => Set(configure, "range");
	public ApiKeyAggregationDescriptor Terms(Elastic.Clients.Elasticsearch.Serverless.Aggregations.TermsAggregation termsAggregation) => Set(termsAggregation, "terms");
	public ApiKeyAggregationDescriptor Terms<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.TermsAggregationDescriptor> configure) => Set(configure, "terms");
	public ApiKeyAggregationDescriptor ValueCount(Elastic.Clients.Elasticsearch.Serverless.Aggregations.ValueCountAggregation valueCountAggregation) => Set(valueCountAggregation, "value_count");
	public ApiKeyAggregationDescriptor ValueCount<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.ValueCountAggregationDescriptor> configure) => Set(configure, "value_count");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (AggregationsValue is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsValue, options);
		}

		if (MetaValue is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, MetaValue, options);
		}

		if (!string.IsNullOrEmpty(ContainedVariantName))
		{
			writer.WritePropertyName(ContainedVariantName);
			if (Variant is not null)
			{
				JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
				writer.WriteEndObject();
				return;
			}

			JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		}

		writer.WriteEndObject();
	}
}